import {addBrand, queryBrand, modBrand, delBrand, queryAllBrand} from '../services/goods';

export default {
    namespace: 'brandEdit',

    state: {
        data: {
            list: [],
            pagination: {},
        },
        brandSList: [],
        loading: true,
    },

    effects: {

        * add({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(addBrand, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });

            if (callback) callback(response);
        },
        * mod({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(modBrand, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });

            if (callback) callback(response);
        },
    },

    reducers: {
        save(state, action) {
            const brandSList = [];
            getBrandSList(brandSList, action.payload.resultData.brandList.list);

            function getBrandSList(tmpR, list) {
                for (var i = 0; i < list.length; i++) {
                    const typeItem = {value: list[i]["brand_id"], label: list[i]["brand_name"]};
                    if (list[i]["children"]) {
                        typeItem["children"] = [];
                        getBrandSList(typeItem["children"], list[i]["children"]);
                    }
                    tmpR.push(typeItem);
                }
            }

            return {
                ...state,
                brandSList: brandSList,
                data: action.payload.resultData.brandList,
            };
        },
        changeLoading(state, action) {
            return {
                ...state,
                loading: action.payload,
            };
        },
    },

};
